package com.example.whereismystuff.views;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.example.whereismystuff.R;
import com.example.whereismystuff.classes.Item;
import com.example.whereismystuff.classes.ItemDataBaseAdapter;
import com.example.whereismystuff.classes.ItemDataManager;
import com.example.whereismystuff.classes.LoginDataBaseAdapter;
import com.example.whereismystuff.classes.User;
import com.example.whereismystuff.classes.UserDataManager;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/**
 * Item viewer calls the databse and gets the items to display on the screen in a custon ncursor adapter.
 * It also takes in a calling activity in the bundle to check whether to get all items or myitems
 * @author NullPointers
 *
 */
public class ItemViewer extends ListActivity {
	String username;
	User curUser;
	ItemDataBaseAdapter mySQLiteAdapter;
	String[] from;
	ArrayList<String> itemnames;
	int count = 0;
	Cursor currentItems;

	ArrayList<Item> currentItemList;
	String[] items;

	/**
	 * Method used to Instantiate Object and set the Layout for the Activity
	 * @param Bundle
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mySQLiteAdapter = new ItemDataBaseAdapter(this);
		mySQLiteAdapter.open();
		Intent i = getIntent();
		Bundle extras = i.getExtras();
		String callingActivity = extras.getString("calling-activity");
		Cursor cursor;
		username = extras.getString("Username");
		//Toast.makeText(this, username, Toast.LENGTH_SHORT).show();

		if(callingActivity!= null && callingActivity.equals("myitems")){
			//Toast.makeText(this, username, Toast.LENGTH_SHORT).show();
			cursor = mySQLiteAdapter.queueAllThisUser(username);
			//Toast.makeText(this, (cursor.getCount()) + "", Toast.LENGTH_SHORT).show();
		}
		else{
			cursor = mySQLiteAdapter.queueAll();
		}
		startManagingCursor(cursor);
		NewListAdapter cursorAdapter = new NewListAdapter(this, cursor);
		// listContent.setAdapter(cursorAdapter);
		setListAdapter(cursorAdapter);
		itemnames = mySQLiteAdapter.queueAllItems();
		currentItems = mySQLiteAdapter.queueAll();
		//mySQLiteAdapter.close();
	}

	/**
	 * On back pressed allows the user to go back to the dashboard screen
	 */
	public void onBackPressed() {
		try{
			Class c = Class.forName("com.example.whereismystuff.views.DashboardView");
			Intent i = getIntent();
			Bundle extras = i.getExtras();
			extras.putString("calling-activity", "all-items");
			//i.putExtra("calling-activity", "myitems");
			i = new Intent(getApplicationContext(), c);
			i.putExtras(extras);
			startActivity(i);
			finish();
		} catch (ClassNotFoundException e){
			e.printStackTrace();
		}
	}

	/**
	 * Creates the menu for the activity
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu){
		new MenuInflater(this).inflate(R.menu.myitem, menu);
		return(super.onCreateOptionsMenu(menu));
	}

	/**
	 * Tells the application which method to call
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		switch (item.getItemId()) {
		case R.id.addItemMenu:
		case R.id.postItem:
			moveToAddItem();
			return true;
		case R.id.logOut:
		case R.id.logOut2:
			moveToLogin();
			return true;
		case R.id.home:
			onBackPressed();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/**
	 * Redirects the user to add item
	 */
	public void moveToAddItem(){
		try{
			Class c = Class.forName("com.example.whereismystuff.views.AddItem");
			//Bundle extras = new Bundle();
			//extras.putString("Username", username);
			Intent i = new Intent(getApplicationContext(), c);
			i.putExtras(getIntent().getExtras());
			startActivity(i);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * logs the user out and returns to the login screen
	 */
	public void moveToLogin(){
		try{
			Class c = Class.forName("com.example.whereismystuff.views.Login");
			Intent intent = new Intent(this, c);
			Bundle extras = new Bundle();
			extras.putString(Login.USERNAME_TAG, username);
			startActivity(intent);
			finish();
		} catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * When an item is clicked, it sends them to the edit item view
	 */
	@Override
	protected void onListItemClick(ListView list, View view, int position,
			long id) {
		super.onListItemClick(list, view, position, id);
		try
		{

			String itemName = itemnames.get(position);
			currentItems.moveToPosition(position);
			mySQLiteAdapter.open();

			String found = currentItems.getString(currentItems.getColumnIndex("FOUND"));
			String name = currentItems.getString(currentItems.getColumnIndex("USERNAME"));
			String id2 = currentItems.getString(currentItems.getColumnIndex("_id"));
			String description = currentItems.getString(currentItems.getColumnIndex("DESCRIPTION"));
			String image = currentItems.getString(currentItems.getColumnIndex("IMAGE"));
			//mySQLiteAdapter.close();
			//String zipCode = mySQLiteAdapter.getSingleEntryZipCode(itemName);
			String zipCode = currentItems.getString(currentItems.getColumnIndex("ZIPCODE"));
			ArrayList<String> tags = new ArrayList<String>();// = mySQLiteAdapter.getSingleEntryTags(itemName);
			tags.add(currentItems.getString(currentItems.getColumnIndex("BOOK")));
			tags.add(currentItems.getString(currentItems.getColumnIndex("ELECTRONIC")));
			tags.add(currentItems.getString(currentItems.getColumnIndex("PERSONAL")));
			ArrayList<String> temp =  new ArrayList<String>();
			temp.add(itemName);
			temp.add(found);
			Bundle dataBundle = new Bundle();
			dataBundle.putString("id", id2);
			dataBundle.putString("Username", username);
			dataBundle.putString("ItemName", itemName);
			dataBundle.putString("Description", description);
			dataBundle.putString("Found", found);
			dataBundle.putString("ZipCode", zipCode);
			dataBundle.putString("Image", image);

			if(tags != null){
				dataBundle.putString("Book", tags.get(0));
				dataBundle.putString("Electronic", tags.get(1));
				dataBundle.putString("Personal", tags.get(2));
			}
			else{
				dataBundle.putString("Book", null);
				dataBundle.putString("Electronic", null);
				dataBundle.putString("Personal", null);
			}
			Intent intent;
			if(username.equals(name)){
				Class c = Class.forName("com.example.whereismystuff.views.EditItem");
				intent = new Intent(this, c);
				
			}
			else{
				Class c = Class.forName("com.example.whereismystuff.views.ViewAnItem");
				intent = new Intent(this, c);
			}
			intent.putExtras(dataBundle);
			startActivity(intent);
		}
		catch(ClassNotFoundException e)
		{
			e.printStackTrace();
		}
	}

	protected void onResume(){
		super.onResume();

	}
}
